const router = require('koa-router')()
//主页模块
router.prefix('/home')

//获取登录日志
router.get('/loginDiary', async (ctx, next) => {
    let countSql = "select count(*) from logindiary";
    let countResults = await ctx.db.EXCUTE(countSql, []);
    let total = countResults[0]["count(*)"];
    let pageSize = Number(ctx.request.query.size)
    let page = ctx.request.query.page
    let startRow = total > 0 ? ((page - 1) * pageSize) : 0;
    //实现分页限制查找
    let limitSql = `select * from logindiary order by id desc limit ${startRow},${pageSize}`;
    let results = await ctx.db.EXCUTE(limitSql, []);
    let obj = {
        length: total,
        results: results,
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//获取总数
router.get('/nums', async (ctx, next) => {
    let countSql1 = "select count(*) from orders";
    let countResults1 = await ctx.db.EXCUTE(countSql1, []);
    let orderTotal = countResults1[0]["count(*)"];
    let countSql2 = "select count(*) from produce";
    let countResults2 = await ctx.db.EXCUTE(countSql2, []);
    let produceTotal = countResults2[0]["count(*)"];
    let countSql3 = "select count(*) from buy";
    let countResults3 = await ctx.db.EXCUTE(countSql3, []);
    let buyTotal = countResults3[0]["count(*)"];

    //入库待处理
    let countSql4 = `select count(*) from requires where require_type = '待处理' and require_kind like '%入库%'`;
    let countResults4 = await ctx.db.EXCUTE(countSql4, []);
    let inTotal = countResults4[0]["count(*)"];


    //出库待处理
    let countSql5 = `select count(*) from requires where require_type = '待处理' and require_kind like '%出库%'`;
    let countResults5 = await ctx.db.EXCUTE(countSql5, []);
    let outTotal = countResults5[0]["count(*)"];

    let obj = {
        orderTotal: orderTotal,
        produceTotal: produceTotal,
        buyTotal: buyTotal,
        inTotal: inTotal,
        outTotal: outTotal,
        status: 200
    }
    ctx.body = JSON.stringify(obj);
})

//获取订单状态所占比
//待分派-处理中-生产完成，待出库-出库请求待处理-已完成
router.get('/orderPercents', async (ctx, next) => {
    let countSql1 = "select count(*) from orders";
    let countResults1 = await ctx.db.EXCUTE(countSql1, []);
    let orderTotal = countResults1[0]["count(*)"];

    let countSql2 = "select count(*) from orders where order_type = '待分派'";
    let countResults2 = await ctx.db.EXCUTE(countSql2, []);
    let num1 = countResults2[0]["count(*)"];
    let percent1 = num1 / orderTotal;

    let countSql3 = "select count(*) from orders where order_type = '处理中'";
    let countResults3 = await ctx.db.EXCUTE(countSql3, []);
    let num2 = countResults3[0]["count(*)"];
    let percent2 = num2 / orderTotal;

    let countSql4 = "select count(*) from orders where order_type = '生产完成,待出库'";
    let countResults4 = await ctx.db.EXCUTE(countSql4, []);
    let num3 = countResults4[0]["count(*)"];
    let percent3 = num3 / orderTotal;

    let countSql5 = "select count(*) from orders where order_type = '出库请求待处理'";
    let countResults5 = await ctx.db.EXCUTE(countSql5, []);
    let num4 = countResults5[0]["count(*)"];
    let percent4 = num4 / orderTotal;

    let countSql6 = "select count(*) from orders where order_type = '已完成'";
    let countResults6 = await ctx.db.EXCUTE(countSql6, []);
    let num5 = countResults6[0]["count(*)"];
    let percent5 = num5 / orderTotal;

    let arr = [
        { type: "待分派", num: num1, percent: percent1 },
        { type: "处理中", num: num2, percent: percent2 },
        { type: "生产完成，待出库", num: num3, percent: percent3 },
        { type: "出库请求待处理", num: num4, percent: percent4 },
        { type: "已完成", num: num5, percent: percent5 },
    ];

    let obj = {
        status: 200,
        arr: arr
    }
    ctx.body = JSON.stringify(obj);

})

//获取生产订单状态所占比
////待分派-待生产员处理中-申请原料中-生产中-入库中-已完成
router.get('/producePercents', async (ctx, next) => {
    let countSql1 = "select count(*) from produce";
    let countResults1 = await ctx.db.EXCUTE(countSql1, []);
    let produceTotal = countResults1[0]["count(*)"];

    let countSql2 = "select count(*) from produce where product_type = '待分派'";
    let countResults2 = await ctx.db.EXCUTE(countSql2, []);
    let num1 = countResults2[0]["count(*)"];
    let percent1 = num1 / produceTotal;

    let countSql3 = "select count(*) from produce where product_type = '待生产员处理中'";
    let countResults3 = await ctx.db.EXCUTE(countSql3, []);
    let num2 = countResults3[0]["count(*)"];
    let percent2 = num2 / produceTotal;

    let countSql4 = "select count(*) from produce where product_type = '申请原料中'";
    let countResults4 = await ctx.db.EXCUTE(countSql4, []);
    let num3 = countResults4[0]["count(*)"];
    let percent3 = num3 / produceTotal;

    let countSql5 = "select count(*) from produce where product_type = '生产中'";
    let countResults5 = await ctx.db.EXCUTE(countSql5, []);
    let num4 = countResults5[0]["count(*)"];
    let percent4 = num4 / produceTotal;

    let countSql6 = "select count(*) from produce where product_type = '入库中'";
    let countResults6 = await ctx.db.EXCUTE(countSql6, []);
    let num5 = countResults6[0]["count(*)"];
    let percent5 = num5 / produceTotal;

    let countSql7 = "select count(*) from produce where product_type = '已完成'";
    let countResults7 = await ctx.db.EXCUTE(countSql7, []);
    let num6 = countResults7[0]["count(*)"];
    let percent6 = num6 / produceTotal;

    let arr = [
        { type: "待分派", num: num1, percent: percent1 },
        { type: "待生产员处理中", num: num2, percent: percent2 },
        { type: "申请原料中", num: num3, percent: percent3 },
        { type: "生产中", num: num4, percent: percent4 },
        { type: "入库中", num: num5, percent: percent5 },
        { type: "已完成", num: num6, percent: percent6 }
    ];

    let obj = {
        status: 200,
        arr: arr
    }
    ctx.body = JSON.stringify(obj);
})

//获取采购订单状态所占比
///待分派-采购中-入库中-已完成
router.get('/buyPercents', async (ctx, next) => {
    let countSql1 = "select count(*) from buy";
    let countResults1 = await ctx.db.EXCUTE(countSql1, []);
    let buyTotal = countResults1[0]["count(*)"];

    let countSql2 = "select count(*) from buy where buy_type = '待分派'";
    let countResults2 = await ctx.db.EXCUTE(countSql2, []);
    let num1 = countResults2[0]["count(*)"];
    let percent1 = num1 / buyTotal;

    let countSql3 = "select count(*) from buy where buy_type = '采购中'";
    let countResults3 = await ctx.db.EXCUTE(countSql3, []);
    let num2 = countResults3[0]["count(*)"];
    let percent2 = num2 / buyTotal;

    let countSql4 = "select count(*) from buy where buy_type = '入库中'";
    let countResults4 = await ctx.db.EXCUTE(countSql4, []);
    let num3 = countResults4[0]["count(*)"];
    let percent3 = num3 / buyTotal;

    let countSql5 = "select count(*) from buy where buy_type = '已完成'";
    let countResults5 = await ctx.db.EXCUTE(countSql5, []);
    let num4 = countResults5[0]["count(*)"];
    let percent4 = num4 / buyTotal;

    let arr = [
        { type: "待分派", num: num1, percent: percent1 },
        { type: "采购中", num: num2, percent: percent2 },
        { type: "入库中", num: num3, percent: percent3 },
        { type: "已完成", num: num4, percent: percent4 },
    ];
    let obj = {
        status: 200,
        arr: arr
    }
    ctx.body = JSON.stringify(obj);
})
module.exports = router